package us.icitap.cm.ui.policecase;

import java.io.Serializable;
import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.entities.PoliceCode;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustListSelect;
import us.icitap.cm.ui.custom.CustWindow;
import us.icitap.utils.Result;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

public class PoliceCodeComponent extends HorizontalLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 494301508698669965L;

	private final CustWindow policeCodeWindow = new CustWindow("Kodi policor");
	
	private final Tree policeCodeTree = new Tree();
	
	private final CustListSelect policeCodeListSelect = new CustListSelect();
	
	private final CustButton addButton = new CustButton("+");
	private final CustButton removeButton = new CustButton("-");
	private final CustButton makePrimaryButton = new CustButton("^");
	
	private  final static BeanItemContainer<PoliceCode> pcContainer = new BeanItemContainer<PoliceCode>(PoliceCode.class);
	public PoliceCodeComponent() {
		
		
		this.setCaption("Kodi policor");
		VerticalLayout vl0 = new VerticalLayout();
		vl0.setSizeUndefined();
		vl0.addComponent(policeCodeListSelect);
		vl0.addComponent(this.addButton);
		vl0.addComponent(this.removeButton);
		vl0.addComponent(this.makePrimaryButton);
		
		this.addButton.setWidth("2em");
		this.removeButton.setWidth("2em");
		this.makePrimaryButton.setWidth("2em");
		this.addComponent(policeCodeListSelect);
		policeCodeListSelect.setContainerDataSource(pcContainer);
		this.addComponent(vl0);
		this.setComponentAlignment(vl0, Alignment.TOP_CENTER);
		policeCodeListSelect.setNullSelectionAllowed(false);
	

		VerticalLayout vl = new VerticalLayout();
		vl.addComponent(policeCodeTree);
		policeCodeWindow.setContent(vl);
		policeCodeWindow.setHeight("350px");
		policeCodeWindow.setWidth("300px");
		final HierarchicalContainer container = new PoliceCodeContainer();
		policeCodeTree.setContainerDataSource(container);
		policeCodeTree.setImmediate(true);
		
		this.addButton.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -8018445249289914747L;

			@Override
			public void buttonClick(ClickEvent event) {
				CaseManagementUI.getCurrent().addWindow(policeCodeWindow);
			}
		});
		
		this.removeButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				pcContainer.removeItem(policeCodeListSelect.getValue());
			}
		});
		
		makePrimaryButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Object item = policeCodeListSelect.getValue();
				pcContainer.removeItem(item);
				pcContainer.addItemAt(0, item);
				
			}
		});
		
		
		policeCodeTree.addItemClickListener(new ItemClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 2658081794686196184L;

			@Override
			public void itemClick(ItemClickEvent event) {
				if(event.isDoubleClick()){
					PoliceCode pc = (PoliceCode) event.getItemId();
					pcContainer.addBean(pc);
					
					policeCodeListSelect.setSizeUndefined();
					
				}
				
			}
		});
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<PoliceCode> getSelectedPoliceCodeList(){
		return (List<PoliceCode>) policeCodeListSelect.getItemIds();
	}
	

	private class PoliceCodeContainer extends HierarchicalContainer implements Serializable{
		
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 7191799770001478759L;

		public PoliceCodeContainer() {
			
			Result<List<PoliceCode>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getRootPoliceCode();
			if(result.isSucceeded()){
				List<PoliceCode> list = result.getObject();
				for (PoliceCode policeCode : list) {
					this.addItem(policeCode);
					rec(policeCode);
				}
			}
			
		}
		
		private void rec(PoliceCode pc){
			for (PoliceCode polCo : pc.getChildrenPoliceCode()) {
				this.addItem(polCo);
				this.setParent(polCo, pc);
				this.rec(polCo);
			}
			
		}
	}
}
